﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SGArchiveSystem7x.App_Code
{
    /// <summary>
    /// session 模块
    /// </summary>
    public class CSession
    {
        #region 清除所有SESSION，退出系统时用
        /// <summary>
        /// 清除所有session,退出系统时用
        /// </summary>
        /// <param name="session"></param>
        public static void Clear_AllSession(HttpSessionStateBase session)
        {
            session.RemoveAll();
        }
        #endregion

        #region 必要的页面级SESSION清除，每到一个新的页面，需要清除一些必要的SESSION内容
        /// <summary>
        /// 清除页面级SESSION，主要是查询条件session等
        /// </summary>
        public static void Clear_PageSession(HttpSessionStateBase session)
        {
            session.Remove(_sSession_QueryWhere);
        }
        #endregion

        #region 普通查询存放对象
        private static string _sSession_QueryWhere = "SgQueryWhere";
        private static string _sSession_QuerySp = "SgQuerySp";
        /// <summary>
        /// 将查询条件存入到SESSION中
        /// </summary>
        /// <param name="sesssion"></param>
        /// <param name="list"></param>
        public static void Add_QueryWhere(HttpSessionStateBase session, List<string> list)
        {
            if (session[_sSession_QueryWhere] != null)
                session.Remove(_sSession_QueryWhere);
            session.Add(_sSession_QueryWhere, list);
        }
        /// <summary>
        /// 将查询条件从SESSION中取出
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List<string> Get_QueryWhere(HttpSessionStateBase session)
        {
            if (session[_sSession_QueryWhere] == null)
                return null;
            else
            {
                var list = session[_sSession_QueryWhere] as List<string>;
                return list;
            }
        }
        public static void Add_QuerySp(HttpSessionStateBase session, List<string> list)
        {
            if (session[_sSession_QuerySp] != null)
                session.Remove(_sSession_QuerySp);
            session.Add(_sSession_QuerySp, list);
        }
        /// <summary>
        /// 将查询条件从SESSION中取出
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List<string> Get_QuerySp(HttpSessionStateBase session)
        {
            if (session[_sSession_QuerySp] == null)
                return null;
            else
            {
                var list = session[_sSession_QuerySp] as List<string>;
                return list;
            }
        }
        #endregion

        #region 登录用户存放对象
        private static string _sSession_User = "SgUser";
        /// <summary>
        /// 将当前登录用户存入SESSION中
        /// </summary>
        /// <param name="session"></param>
        /// <param name="user"></param>
        public static void Add_User(HttpSessionStateBase session, SGArchiveSystem7xDB.V_User user)
        {
            if (session[_sSession_User] != null)
                session.Remove(_sSession_User);
            session.Add(_sSession_User, user);
        }
        /// <summary>
        /// 从SESSION中获取当前登录用户
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static SGArchiveSystem7xDB.V_User Get_User(HttpSessionStateBase session)
        {

            if (session[_sSession_User] == null)
                return null;
            else
            {
                var user = session[_sSession_User] as SGArchiveSystem7xDB.V_User;
                return user;
            }
        }
        #endregion

        #region 借阅档案的存放对象
        private static string _sSession_Brr = "SgQueryBrr";
        /// <summary>
        /// 将借阅内容存放到SESSION中
        /// </summary>
        /// <param name="session"></param>
        /// <param name="list"></param>
        public static void Add_Brr(HttpSessionStateBase session, List<Models.CBrrModel> list)
        {
            if (session[_sSession_Brr] != null)
                session.Remove(_sSession_Brr);
            session.Add(_sSession_Brr, list);
        }

        /// <summary>
        /// 将借阅内容从SESSION中取出
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List<Models.CBrrModel> Get_Brr(HttpSessionStateBase session)
        {
            if (session[_sSession_Brr] == null)
                return null;
            else
            {
                var list = session[_sSession_Brr] as List<Models.CBrrModel>;
                return list;
            }
        }
        #endregion

        #region 部门权限
        private static string _sSession_Bm = "SgQueryBm";
        /// <summary>
        /// 将当前此人的部门信息存放到SESSION中
        /// </summary>
        /// <param name="session"></param>
        /// <param name="list"></param>
        public static void Add_Bm(HttpSessionStateBase session, SGArchiveSystem7xDB.T_Deparetment bm)
        {
            if (session[_sSession_Bm] != null)
                session.Remove(_sSession_Bm);
            session.Add(_sSession_Bm, bm);
        }

        /// <summary>
        /// 将当前此人的部门信息从SESSION中取出
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static SGArchiveSystem7xDB.T_Deparetment Get_Bm(HttpSessionStateBase session)
        {
            if (session[_sSession_Bm] == null)
                return null;
            else
            {
                var en = session[_sSession_Bm] as SGArchiveSystem7xDB.T_Deparetment;
                return en;
            }
        }
        #endregion


        #region 全部用户判定
        /// <summary>
        /// 添加用户要全局缓存里
        /// </summary>
        /// <param name="session"></param>
        /// <param name="en">true 代表正常， false 代表 这是新的</param>
        /// <returns></returns>
        public static void Add_AppUser(HttpSessionStateBase session, SGArchiveSystem7xDB.V_User en)
        {
            /////添加全局存储
            List<string[]> list = new List<string[]>();
            if (System.Web.HttpContext.Current.Application["SgOnline"] != null)
            {
                list = System.Web.HttpContext.Current.Application["SgOnline"] as List<string[]>;
                foreach (var ss in list)
                {
                    ////如果已经有此用户ID，那么
                    if (ss[1] == en.U_ID.ToString())
                    {
                        ///判断此SESSION ID 是否是同一个
                        if (ss[0] == session.SessionID)
                        {
                            ////如果不是，那么删除原来的，添加到新的里面
                            list.Remove(ss);
                            System.Web.HttpContext.Current.Application.Lock();
                            System.Web.HttpContext.Current.Application["SgOnline"] = list;
                            System.Web.HttpContext.Current.Application.UnLock();
                            return;
                        }
                    }
                }
            }
            ////将本计算机的SESSION ID与登录的ID一起存进去
            list.Add(new string[] { session.SessionID, en.U_ID.ToString() });
            System.Web.HttpContext.Current.Application.Lock();
            System.Web.HttpContext.Current.Application["SgOnline"] = list;
            System.Web.HttpContext.Current.Application.UnLock();
        }

        /// <summary>
        /// 判断当前用户是否是全局变量的最后次记录的SESSION ID，不是那么强制其退出系统
        /// </summary>
        /// <param name="session"></param>
        /// <returns>-1. 非最后次登录的SESSION， 0. SESSION丢失,  1. 正常</returns>
        public static int Vaild_AppUser(HttpSessionStateBase session)
        {
            var user = Get_User(session);
            if(user == null)
            {
                return 0;
            }
            else
            {
                var list = System.Web.HttpContext.Current.Application["SgOnline"] as List<string[]>;
                if(list == null)
                {
                    list.Add(new string[] { session.SessionID, user.U_ID.ToString() });
                    System.Web.HttpContext.Current.Application.Lock();
                    System.Web.HttpContext.Current.Application["SgOnline"] = list;
                    System.Web.HttpContext.Current.Application.UnLock();
                    return 1;
                }
                else
                {
                    foreach (var ss in list)
                    {
                        ////如果已经有此用户ID，那么
                        if (ss[1] == user.U_ID.ToString())
                        {
                            ///判断此SESSION ID 是否是同一个
                            if (ss[0] == session.SessionID)
                                return 1;
                            else
                                return -1;
                        }
                    }

                    list.Add(new string[] { session.SessionID, user.U_ID.ToString() });
                    System.Web.HttpContext.Current.Application.Lock();
                    System.Web.HttpContext.Current.Application["SgOnline"] = list;
                    System.Web.HttpContext.Current.Application.UnLock();
                    return 1;
                }
            }
        }
        #endregion
    }
}